package com.zhoug.adcommon3demo;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.zhoug.adcommon3demo.fileselector.TestFileSelectorActivity;
import com.zhoug.common3.adapters.recycler.dividers.ListItemDecoration;
import com.zhoug.common3.adapters.recycler2.BaseRecyclerViewAdapter;
import com.zhoug.common3.adapters.recycler2.BaseViewHolder;
import com.zhoug.common3.router.Router;

import java.util.Arrays;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class MainActivity extends MyBaseActivity {
    private static final List<ActivityUrl> URLS= Arrays.asList(
            new ActivityUrl("拨打电话",CallPhoneActivity.class),
            new ActivityUrl("文件选择器", TestFileSelectorActivity.class)
    );

    private RecyclerView mRecyclerView;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void findViews() {

        mRecyclerView = findViewById(R.id.recyclerView);
    }

    @Override
    protected void onCreate1(Bundle savedInstanceState) {
        UrlAdapter adapter=new UrlAdapter(this);
        adapter.setData(URLS);
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

    }

    private static final class UrlAdapter extends BaseRecyclerViewAdapter<ActivityUrl> implements View.OnClickListener {

        public UrlAdapter(Context context) {
            super(context);
        }

        @Override
        protected int getLayoutId(int viewType) {
            return R.layout.main_test_menu_item;
        }

        @Override
        protected void onBindData(BaseViewHolder holder, ActivityUrl data, int position, int viewType) {
            TextView mTvName = holder.itemView.findViewById(R.id.tv_name);
            if(data!=null){
                mTvName.setText(data.getName());
                holder.itemView.setTag(position);
                holder.itemView.setClickable(true);
                holder.itemView.setOnClickListener(this);
            }
        }


        @Override
        public void onClick(View v) {
            ActivityUrl itemData = getItemData((Integer) v.getTag());
            if(null != itemData){
                Router.navigation(getContext(),itemData.getCls());
            }
        }
    }

    private static final class ActivityUrl{
        private final String name;
        private final Class<? extends Activity> cls;

        public ActivityUrl(String name, Class<? extends Activity> cls) {
            this.name = name;
            this.cls = cls;
        }

        public String getName() {
            return name;
        }

        public Class<? extends Activity> getCls() {
            return cls;
        }

    }

}